<template>
  <div class="articles">
    <!-- <banner></banner> -->
    <div class="site-content animate">

      <main class="site-main">
        <article class="hentry">
          <!-- 文章头部 -->
          <header class="entry-header" v-if="!$store.getters.loading">
            <!-- 标题输出 -->
            <h1 class="entry-title">{{blogData.blogDto.title}}</h1>
            <hr>
            <div class="breadcrumbs">
              <ul>
                <li>
                  <i class="iconfont icon-user"></i>
                  <a href="javascript:void(0);">{{blogData.blogDto.author == null ? blogData.userDto.username : blogData.blogDto.author}}</a>
                </li>
                <li>
                  <i class="iconfont icon-fenlei"></i>
                  <a href="javascript:void(0);" @click="toCategoryArticle(blogData.blogDto.typeId,getBlogType(blogData.blogDto.typeId))" v-text="getBlogType(blogData.blogDto.typeId)"></a>
                </li>
                <li>
                  <i class="iconfont icon-shijian"></i>
                  <a href="javascript:void(0);">{{blogData.blogDto.createTime}}</a>
                </li>
                <li>
                  <i class="iconfont icon-eyes"></i>
                  <a href="javascript:void(0);">{{blogData.blogDto.readCount}}</a>
                </li>
                <li>
                  <i class="iconfont icon-dianzan"></i>
                  <a href="javascript:void(0);">{{blogData.blogDto.goodCount}}</a>
                </li>
              </ul>
              <div class="tags">
                <a v-if="blogData.blogDto.tagIds" v-for="item in tagIdList" :key="item" href="javascript:void(0);" @click="toTagArticle(item,getTag(item))">{{getTag(item)}}</a>
              </div>
            </div>
          </header>
          <!-- 正文输出 -->
          <div v-images-loaded.on="imageProgress" id="content" class="entry-content" v-html="blogData.blogDto.blogContent" v-highlight></div>
          <!-- 文章底部 -->
          <section-title>
            <footer class="post-footer">
              <!-- 分享按钮 -->
              <!--                        <div class="post-share">-->
              <!--                            <ul class="sharehidden">-->
              <!--                                <li><a href="http://qr.liantu.com/api.php?text=https://zhebk.cn/Web/gongan.html"-->
              <!--                                       onclick="window.open(this.href, 'renren-share', 'width=490,height=700');return false;"-->
              <!--                                       class="s-weixin" target="_blank" rel="nofollow noopener noreferrer"><img src="https://cdn.zhebk.cn/usr/themes/Akina/images/wechat.png"></a></li>-->
              <!--                                <li>-->
              <!--                                    <a href="http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=https://zhebk.cn/Web/gongan.html/&amp;title=公安联网备案记录"-->
              <!--                                       onclick="window.open(this.href, 'weibo-share', 'width=730,height=500');return false;"-->
              <!--                                       class="s-qq" target="_blank" rel="nofollow noopener noreferrer"><img src="https://cdn.zhebk.cn/usr/themes/Akina/images/qzone.png"></a></li>-->
              <!--                                <li>-->
              <!--                                    <a href="http://service.weibo.com/share/share.php?url=https://zhebk.cn/Web/gongan.html/&amp;title=公安联网备案记录"-->
              <!--                                       onclick="window.open(this.href, 'weibo-share', 'width=550,height=235');return false;"-->
              <!--                                       class="s-sina" target="_blank" rel="nofollow noopener noreferrer"><img src="https://cdn.zhebk.cn/usr/themes/Akina/images/sina.png"></a></li>-->
              <!--                                <li>-->
              <!--                                    <a href="http://shuo.douban.com/!service/share?https://zhebk.cn/Web/gongan.html/&amp;title=公安联网备案记录"-->
              <!--                                       onclick="window.open(this.href, 'renren-share', 'width=490,height=600');return false;"-->
              <!--                                       class="s-douban" target="_blank" rel="nofollow noopener noreferrer"><img src="https://cdn.zhebk.cn/usr/themes/Akina/images/douban.png"></a></li>-->
              <!--                            </ul>-->
              <!--                            <i class="iconfont show-share"></i>-->
              <!--                        </div>-->

              <!-- 赞助按钮 -->
              <!-- <div class="donate" @click="showDonate=!showDonate"> -->
              <!-- <span>赏</span>
                <ul class="donate_inner" :class="{'show':showDonate}">
                  <li class="wedonate"><img src="http://cdn.fengziy.cn/gblog/wexin_pay.png">
                    <p>微信</p>
                  </li>
                  <li class="alidonate"><img src="http://cdn.fengziy.cn/gblog/ali_pay.jpg">
                    <p>支付宝</p>
                  </li>
                </ul> -->
              <!-- </div> -->
              <!-- 点赞按钮 -->
              <div class="like" v-if="!$store.getters.loading">
                <p class="diggit" @click="good(blogData.blogDto.id)">
                  <a href="javascript:void(0);">很赞哦!</a>
                  <span v-if="blogData.blogDto.goodCount!= 0">
                    (<b>{{blogData.blogDto.goodCount}}</b>)
                  </span>
                </p>
              </div>
              <div class="donate" v-if="!$store.getters.loading">
                <p class="diggit" @click="good(blogData.blogDto.id)">
                  <a href="javascript:void(0);">打赏本站!</a>
                </p>
              </div>
            </footer>
          </section-title>

          <!--声明-->
          <div class="open-message" v-if="!$store.getters.loading">
            <p>{{blogData.blogDto.copyright}}</p>
          </div>
          <!--评论-->
          <!-- <div class="comments">
            <comment v-for="item in comments" :key="item.comment.id" :comment="item.comment">
              <template v-if="item.reply.length">
                <comment v-for="reply in item.reply" :key="reply.id" :comment="reply"></comment>
              </template>
            </comment>
          </div> -->
        </article>
      </main>

      <!-- 目录和文章推荐 -->
      <div class="side-rightbar">
        <side-catalog v-if="showSideCatelog" ref="catalog" class="catalog" v-bind="catalogProps"></side-catalog>
        <Recommend class="otherDiv" :style="{position: 'sticky',top:offsetHeight+'px'}"></Recommend>
      </div>
    </div>
  </div>
</template>

<script>
import { getBlogByUid , goodBlog } from "../api/blogContent"
import { getTagAndCount  } from "../api/tag"
import Banner from '@/components/banner'
import sectionTitle from '@/components/section-title'
import comment from '@/components/comment'
import menuTree from '@/components/menu-tree'
import { fetchComment } from '../api'
import { getAll } from '../api/category'
import Recommend from '@/components/recommend'
//目录组件
import SideCatalog from 'vue-side-catalog'
import 'vue-side-catalog/lib/vue-side-catalog.css'
//图片加载监听指令
import imagesLoaded from 'vue-images-loaded'
export default {
  name: 'articles',
  components: {
    Banner,
    sectionTitle,
    comment,
    menuTree,
    Recommend,
    SideCatalog
  },
  directives: {
    imagesLoaded,
  },
  data() {
    return {
      catalogProps: {
        container: '#content',
        watch: true,
        // lineLeft: 0,
        lineShow: false,
        levelGap: 15
      },
      offsetHeight: 0,  //目录下面的div的y轴偏移量
      showSideCatelog: false,
      blogId: null,
      blogData: {
        blogSort: {}
        ,blogDto: {}
        ,userDto: {}
      },
      tagIdList: [],
      blogTypeList: [],
      blogTagList: [],
      showDonate: false,
      comments: [],
      menus: []
    }
  },
  watch: {
    //监测目录生成后重新刷新一遍目录的偏移量
    offsetHeight: function () {
      this.$nextTick(function () {
        /*现在数据已经渲染完毕*/
        this.imageProgress(); //数据加载完成之后再重新刷新一遍目录的偏移量
      })
    }
  },
  methods: {
    //监听对象存储的图片加载完后重新计算目录的偏移量
    imageProgress(instance) {
      this.$refs.catalog.setTopList();
    },
    good(id){
      goodBlog({'id':id}).then(resp =>{
        if(resp.data.code !== 200){
          alert('点赞失败！'+resp.data.msg)
          return
        }
        this.blogData.blogDto.goodCount++
        alert('点赞成功，多谢支持！')
      })
    },
    getBlogInfo() {
      this.$store.dispatch('setLoading', true);
      var params = new URLSearchParams();
      params.append("id", this.blogId);
      getBlogByUid(params).then(response => {
        if(response.data.code !== 200){
          alert(response.data.msg)
          return
        }
        this.blogData = response.data.data;
        if(this.blogData.blogDto.tagIds){
          var idStrs = this.blogData.blogDto.tagIds.split(',')
          for(let str of idStrs){
            this.tagIdList.push(parseInt(str))
          }
        }

        this.showSideCatelog = true;
        setTimeout(() => {
          this.offsetHeight = this.$refs.catalog.$el.offsetHeight + 100;
          this.$store.dispatch('setLoading', false);
        })
      });
    },
    getComment() {
      fetchComment().then(res => {
        this.comments = res.data || []
      }).catch(err => {
        console.log(err)
      })
    },
    fetchH(arr, left, right) {
      if (right) {
        return arr.filter(item => item.offsetTop > left && item.offsetTop < right)
      } else {
        return arr.filter(item => item.offsetTop > left)
      }
    },
    toCategoryArticle(id,name) {
      this.$router.push({ name: 'categoryArticle', params: { typeId: id, typeName: name } }).catch(data => { });  //最后的catch是为了防止重复push相同的路由报错警告
    },
    toTagArticle(id,tagName) {
      this.$router.push({ name: 'tagArticle', params: { tagId:id, tagName: tagName } }).catch(data => { });  //最后的catch是为了防止重复push相同的路由报错警告  //最后的catch是为了防止重复push相同的路由报错警告
    },
    getBlogTypeList() {
      getAll().then(res => {
        let result = res.data
        if(result.code === 200)
          this.blogTypeList = result.data.result || []
        else
          alert(result.msg)
      })
    },
    getBlogTagList() {
      getTagAndCount().then(res => {
        let result = res.data
        if(result.code === 200)
          this.blogTagList = result.data.result || []
        else
          alert(result.msg)
      })
    },
    getBlogType(id){
      for (var blogType of this.blogTypeList) {
        if(blogType.id === id)
          return blogType.name;
      }
    },getTag(id){
      for (var tag of this.blogTagList) {
        if(tag.id === id)
          return tag.name;
      }
    }
  },
  mounted() {
    this.blogId = this.$route.params.id;
    this.getBlogInfo()
    this.getBlogTypeList()
    this.getBlogTagList()
  },
  created() {
    this.getComment()
  }
}
</script>
<style scoped>
.entry-content >>> img {
  display: block;
  margin: 0 auto;
}
</style>

<style scoped lang="less">
.site-content {
  display: flex;
  justify-content: space-between;
  max-width: 1200px;
  margin-top: 70px;
  position: relative;
  .site-main {
    width: 73%;
  }
  .side-rightbar {
    width: 25%;
    .catalog {
      margin-top: 10px;
      box-shadow: 0 2px 12px 0 rgba(128, 128, 128, 0.1);
      border-radius: 5px;
      width: 295px;
      max-height: 470px;
      font-size: 14px;
      position: fixed;
    }
    .otherDiv {
      box-shadow: 0 2px 12px 0 rgba(128, 128, 128, 0.1);
      border-radius: 5px;
    }
  }
}

// #article-menus {
//   position: sticky;
//   top: 0;
//   box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
//   border-radius: 3px;
//   padding: 15px;
//   width: 300px;
//   transform: translateX(-120%) translateY(80px);
//   font-size: 14px;
// }
article.hentry {
  .entry-header {
    .entry-title {
      font-size: 25px;
      font-weight: 600;
      color: #737373;
      margin: 0.67em 0;

      &:before {
        content: "#";
        margin-right: 6px;
        color: #d82e16;
        font-size: 20px;
        font-weight: 600;
      }
    }

    hr {
      height: 1px;
      border: 0;
      background: #efefef;
      margin: 15px 0;
    }

    .breadcrumbs {
      font-size: 16px;
      color: #d2d2d2;
      text-decoration: none;
      margin-bottom: 30px;
      ul li {
        color: #748594;
        line-height: 1.5;
        display: inline-block;
        padding-right: 30px;
        i {
          padding-right: 5px;
        }
      }
      .tags {
        margin-top: 15px;
        a {
          border-radius: 5px;
          background: #f4650e;
          padding: 4px 8px;
          margin-right: 8px;
          color: #fff;
        }
      }
    }
  }

  footer.post-footer {
    text-align: center;
    margin: 0 auto;
    width: 100%;
    padding: 20px 10px;
    margin-top: 30px;
    height: 65px;
    position: relative;
    i {
      font-size: 18px;
      margin-right: 5px;
    }

    .post-share {
      float: right;
      list-style: none;
      margin-right: 20px;
    }

    // .donate {
    //   float: left;
    //   line-height: 36px;
    //   border-radius: 100%;
    //   -webkit-border-radius: 100%;
    //   -moz-border-radius: 100%;
    //   border: 1px solid #2b2b2b;
    //   &:hover {
    //     border: 1px solid goldenrod;
    //     span {
    //       color: goldenrod;
    //     }
    //   }
    //   span {
    //     color: #2b2b2b;
    //     padding: 10px;
    //     position: relative;
    //     cursor: pointer;
    //   }

    //   .donate_inner {
    //     display: none;
    //     margin: 0;
    //     list-style: none;
    //     position: absolute;
    //     left: 80px;
    //     top: -40px;
    //     background: #fff;
    //     padding: 10px;
    //     border: 1px solid #ddd;
    //     box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
    //     border-radius: 3px;
    //     &.show {
    //       display: block;
    //     }
    //     li {
    //       float: left;
    //     }

    //     img {
    //       width: 100px;
    //     }
    //     p {
    //       text-align: center;
    //       font-size: 15px;
    //       color: #d2d2d2;
    //       line-height: 1rem;
    //     }
    //   }

    //   .donate_inner:after,
    //   .donate_inner:before {
    //     content: "";
    //     position: absolute;
    //     left: 0;
    //     bottom: 45%;
    //     margin-left: -8px;
    //     border-top: 8px solid transparent;
    //     border-bottom: 8px solid transparent;
    //     border-right: 8px solid #fff;
    //   }

    //   .donate_inner:before {
    //     left: -1px;
    //     border-right: 8px solid #ddd;
    //   }
    // }

    .like,
    .donate {
      cursor: pointer;
      display: inline-block;
      margin: 0 80px;
      width: 130px;
      background-color: #e2523a;
      box-shadow: 1px 2px 6px 0px rgba(0, 0, 0, 0.2);
      border-radius: 5px;
      line-height: 40px;
      text-align: center;
      a,
      span {
        color: #fff;
      }
    }
  }
  .open-message {
    margin: 50px 0;
    position: relative;
    background: #2b2b2b;
    padding: 10px 30px;
    border-radius: 3px;
    font-size: 14px;
    color: #fff;
    &:after {
      content: "";
      border-left: 10px solid transparent;
      border-right: 10px solid transparent;
      border-bottom: 10px solid #2b2b2b;
      position: absolute;
      top: -8px;
      left: 48%;
    }
    p {
      margin: 10px 0;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    a {
      color: #a0dad0;
      padding: 0 5px;
    }
  }
}
// .dasbox { width: 130px; float: left; margin-left: 40px; background: url(../images/dsbz.jpg) no-repeat left 20px center #E2523A; box-shadow: 1px 2px 6px 0px rgba(0,0,0,.2); border-radius: 3px; line-height: 40px; padding-left: 10px; text-align: center; }
// .hide_box { z-index: 999; filter: alpha(opacity=50); background: #666; opacity: 0.5; -moz-opacity: 0.5; left: 0; top: 0; height: 99%; width: 100%; position: fixed;}
// .shang_box { width: 430px; padding: 40px 10px; background: #fff url(../images/tbg.jpg) no-repeat left top 30px; ; border-radius: 10px; position: fixed; z-index: 1000; left: 50%; top: 50%; margin-left: -280px; margin-top: -280px; border: 1px dotted #dedede; }
// .shang_box img { border: none; border-width: 0; }
// .dasbox a { color: #fff; }
</style>
